﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：Ajax服务端路由程序，直接映射到对应的逻辑层方法上。
  //创建人：赵国伟
  //生成时间：2012/04/05 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Reflection;

using Shopping.Common;
using Shopping.Model;

namespace Shopping.Ajax
{

    public class AjaxRouteHandler : IHttpHandler
    {

        private ReturnValue ret = new ReturnValue(); //存储返回对象；
             
        public void ProcessRequest(HttpContext context)
        {
            string Action = Utility.G("Action").Trim().ToLower();
            switch (Action)
            {
                case "save":   //保存数据    by 赵国伟 2012-04-19
                    Save(context);
                    break;
                case "query":  //执行数据查询； by 赵国伟 2012-04-19
                    Query(context);
                    break;
                default:       //执行指定的方法； by 赵国伟 2012-04-19
                    Execte(context);
                    break;
            }
        }

        #region "执行数据查询； by 赵国伟 2012-04-19"
        /// <summary>
        /// 执行数据查询；
        /// </summary>
        private void Query(HttpContext context)
        {

        }
        #endregion

        #region "保存数据 by 赵国伟 2012-04-19"
        /// <summary>
        /// 保存数据
        /// </summary>
        private void Save(HttpContext context)
        {

        }
        #endregion

        #region "执行指定的方法； by 赵国伟 2012-04-19"
        /// <summary>
        /// 执行指定的方法；
        /// </summary>
        private void Execte(HttpContext context)
        {
            int pkID = Utility.G<int>("PKID"); //主键ID;
            string nameSpace = "Shopping.BLL";
            string className = "StoresManage";
            string fullName = nameSpace + "." + className;
            string methodName = "GetStoreInfo";
            pkID = 1;
            Assembly app = Assembly.Load(nameSpace);
            Type classType = app.GetType(fullName);

            object bllObj = app.CreateInstance(fullName); //创建该业务对象的示例对象；
            MethodInfo method = classType.GetMethod(methodName); //查找执行反射的方法；

            Stores s = (Stores)method.Invoke(bllObj, new object[] { pkID }); //执行反射方法；
            context.Response.Write(JSONHelper.ObjectToJson<Stores>(s));
        }
        #endregion


        #region "判断指定的方法是否可以被访问 by 赵国伟 2012-04-07"
        /// <summary>
        /// 判断指定的方法是否可以被访问
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        private bool IsPermitAccess(MethodInfo m)
        {
            AjaxMethodAttribute[] attrs = Attribute.GetCustomAttributes(m, typeof(AjaxMethodAttribute)) as AjaxMethodAttribute[] ;
            if (attrs != null)
            {
                foreach (AjaxMethodAttribute attr in attrs)
                {
                    if (attr.MethodType == AjaxMethodType.Common)
                        return true;
                    else if (attr.MethodType == AjaxMethodType.User)
                    {
                        if (Utility.GetCookies(CConst.Cookie_User_ID) != "" || Utility.GetCookies(CConst.Cookie_Employee_ID) != "")
                            return true;
                    }
                    else if (attr.MethodType == AjaxMethodType.Manager)
                    {
                        if (Utility.GetCookies(CConst.Cookie_Employee_ID) != "")
                            return true;
                    }
                }
            }
            return false;
        }
        #endregion




        public bool IsReusable
        {
            get { return true; }
        }
    }
}
